<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;

class Shop_cartController extends Controller
{
    //加入购物车,未登录不允许进购物车
    public function __construct()
    {
        $this -> middleware('adminLogin');
    }

    // 加入购物车
    public function tocart($id)
    {   
        $data = DB::table('shop_type as c1') 
        -> select('c1.*', 'c2.name as pname', DB::raw("concat(c1.path, ',', c1.id) as sortpath"))
        -> leftJoin('shop_type as c2', 'c1.pid', '=', 'c2.id')
        -> orderBy('sortpath')
        -> get();

        $cateData = DB::table('shop_goods') -> where('id', $id) -> first();


        foreach($data as $key => $value){
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|---', $num);
            $data[$key] -> name = $str.$value -> name;
        }
        //购物车数据进入数据库
        $cartData['gid'] = $id;
        $cartData['user_id'] = session('user_master') -> id;

        $cartData['cnt'] = 1;
        $time = date('Y-m-d H:i:s');
        $cartData['created_at'] = $time;
        $cartData['updated_at'] = $time;

        $res = DB::table('shop_cart') -> insert($cartData);
        if($res)
        {
            return view('home.cart.precart', ['title' => '加入购物车'], ['data' => $data, 'cateData' => $cateData]);
        }
    }    

    // ajax价格刷新购物车
    public function ajaxcart(Request $request)
    {
        // $cnt = $request -> input('cnt');   
        // return $cnt;
        $id = $request -> input('id');;

        if(empty($request -> input('cnt')))
        {
            $cartData['cnt'] = 1;
        }else
        {
            $cartData['cnt'] = $request -> input('cnt');
        }

        $time = date('Y-m-d H:i:s');
        $cartData['updated_at'] = $time;

        $res = DB::table('shop_cart') -> where('gid', '=', $id) -> update($cartData);
        //////////////////////////////////////////
        $data = DB::table('shop_cart') -> where('user_id', '=', session('user_master') -> id) -> get();
        
        $bcnt = [];
        $str = '';
        foreach ($data as $key => $value) {
            $str .= $value -> gid.',';
            $bcnt[] = $value -> cnt;
        }
        $str = rtrim($str, ',');

        $data = DB::select('select * from shop_goods where id in ('.$str.')');

        $sum = [];
        $total = '';
        foreach ($data as $key => $value) {
            $total += $value -> price * $bcnt[$key];
            $sum['total'] = $total;
            $sum[] = $value -> price * $bcnt[$key];
        }
        //////////////////////////////////////////

        if($res)
        {
            return $sum;
        }else
        {
            return 2;
        }
        
    }


    // 显示购物车中的商品
    public function index()
    {
    	// $cartData['gid'] = 8;
    	// $cartData['user_id'] = session('user_master') -> id;

     //    $cartData['cnt'] = 1;
     //    $time = date('Y-m-d H:i:s');
     //    $cartData['created_at'] = $time;
     //    $cartData['updated_at'] = $time;

        // $res = DB::table('shop_cart') -> insert($cartData);
        // if($res)
        // {

            $data = DB::table('shop_cart') -> where('user_id', '=', session('user_master') -> id) -> get();
            
            $bcnt = [];
            $str = '';
            foreach ($data as $key => $value) {
                $str .= $value -> gid.',';
                $bcnt[] = $value -> cnt;
            }
            $str = rtrim($str, ',');

            $data = DB::select('select * from shop_goods where id in ('.$str.')');

            $sum = [];
            $total = '';
            foreach ($data as $key => $value) {
                $total += $value -> price * $bcnt[$key];
                $sum['total'] = $total;
                $sum[] = $value -> price * $bcnt[$key];
            }
            return view('home.cart.index', ['title' => '购物车', 'data' => $data, 'sum' => $sum, 'bcnt' => $bcnt]);

        
        // }
    	
    }



}
